package com.hfjyz.registration.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.hfjyz.registration.command.AdminLoginCommand;
import com.hfjyz.registration.command.OfficeListCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.service.OfficeService;

public class OfficeListController extends SimpleFormController {

	private OfficeService officeService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		HttpSession session = request.getSession();
		Object cmd = session.getAttribute(getFormSessionAttributeName(request));
		if (cmd != null) {
			return cmd;
		}
		return super.formBackingObject(request);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#
	 * referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object,
	 * org.springframework.validation.Errors, int)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		OfficeListCommand cmd = (OfficeListCommand) command;
		String eid = request.getParameter("examId");
		if (eid != null && eid.length() > 0) {
			try {
				cmd.setExamId(Integer.valueOf(eid));
			} catch (Exception e) {
			}
		}
		String did = request.getParameter("departId");
		if (did != null && did.length() > 0) {
			try {
				cmd.setDepartId(Integer.valueOf(did));
			} catch (Exception e) {
			}
		}
		return officeService.initSearch(cmd, errors);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		OfficeListCommand cmd = (OfficeListCommand) command;
		request.getSession().setAttribute(getFormSessionAttributeName(), cmd);
		Map model = errors.getModel();
		if (request.getParameter("actionEditO") != null) {
			RedirectView rv = new RedirectView("officeEdit.action");
			rv.setExposeModelAttributes(true);
			model.put("examId", cmd.getExamId());
			model.put("id", cmd.getOid());
			return new ModelAndView(rv, model);
		} else if (request.getParameter("btnAdd") != null) {
			RedirectView rv = new RedirectView("officeEdit.action");
			rv.setExposeModelAttributes(true);
			model.put("examId", cmd.getExamId());
			return new ModelAndView(rv, model);
		} else if (request.getParameter("actionDeleteO") != null) {
			AdminLoginCommand adminCmd = (AdminLoginCommand) request
					.getSession().getAttribute("AdminInfo");
			officeService.loggin(request, ExamContants.LOGING_TYPE_INFO,
					(Integer) null, ExamContants.LOGING_USER_TYPE_MANAGE,
					adminCmd.getAdminId(), ExamContants.LOGING_OP_TYPE_DELETE,
					"岗位删除");
			officeService.delete(request, cmd, errors);
		}
		officeService.search(request, cmd);
		return new ModelAndView(getFormView(), model);
	}

	public OfficeService getOfficeService() {
		return officeService;
	}

	public void setOfficeService(OfficeService officeService) {
		this.officeService = officeService;
	}

}